๋ฌธ์
Programmers - ์ ๊ณ ๊ฒฐ๊ณผ ๋ฐ๊ธฐ
๐ Logic
// ์ ์ ๋ณ ์ ๊ณ ๋นํ ํ์ ์ ์ฅ map
Map<String, Integer> reportMap = new HashMap<>();
// ์ ์ ๋ณ ์ ๊ณ ํ ์ ์ ์ ์ฅ map
Map<String, ArrayList<String>> userReportMap = new HashMap<>();
// ์ ์ ๋ณ ๋ฉ์ผ ๋ฐ์ ํ์ ์ ์ฅ map
Map<String, Integer> mailMap = new HashMap<>();
- ์ ์ ๋ณ๋ก, ์ ๊ณ ๋นํ ํ์์ ์ ๊ณ ํ ์ ์ ๋ฆฌ์คํธ, ๊ทธ๋ฆฌ๊ณ ์ ์ง ๊ธฐ์ค์ ๋์ ์ ์ ๋ฅผ ์ ๊ณ ํ ํ์๋ฅผ ์ ์ฅํ๋ map์ ์ฌ์ฉํ๋ค.
๐ CODE
์ฝ๋ ๋ณด๊ธฐ/์ ๊ธฐ๐ซ
import java.util.*;
class Solution {
// ์ ์ ๋ณ ์ ๊ณ ๋นํ ํ์ ์ ์ฅ map
Map<String, Integer> reportMap = new HashMap<>();
// ์ ์ ๋ณ ์ ๊ณ ํ ์ ์ ์ ์ฅ map
Map<String, ArrayList<String>> userReportMap = new HashMap<>();
// ์ ์ ๋ณ ๋ฉ์ผ ๋ฐ์ ํ์ ์ ์ฅ map
Map<String, Integer> mailMap = new HashMap<>();
public int[] solution(String[] id_list, String[] report, int k) {
for (String rep : report) {
String[] line = rep.split(" ");
String id = line[0];
String bannedId = line[1];
ArrayList<String> list = null;
// ์ ์ ๋ณ ์ ๊ณ ํ ์ ์ ์ ์ฅ
if (userReportMap.containsKey(id)) {
list = userReportMap.get(id);
if (!list.contains(bannedId)) {
list.add(bannedId);
reportMap.put(bannedId, reportMap.getOrDefault(bannedId, 0) + 1);
}
} else {
list = new ArrayList<>();
list.add(bannedId);
reportMap.put(bannedId, reportMap.getOrDefault(bannedId, 0) + 1);
}
userReportMap.put(id, list);
}
// ์ ์ ๋ณ ์ ๊ณ ํ ์ ์ ๊ฐ ์ ์ง ๊ธฐ์ค์ ๋์ ํ์๋ฅผ mailMap์ ์ ์ฅ
for (Map.Entry<String, ArrayList<String>> entry : userReportMap.entrySet()) {
ArrayList<String> list = entry.getValue();
for (String id : list)
if (reportMap.get(id) >= k)
mailMap.put(entry.getKey(), mailMap.getOrDefault(entry.getKey(), 0) + 1);
}
int[] answer = new int[id_list.length];
for (int i = 0; i < id_list.length; i++)
answer[i] = mailMap.getOrDefault(id_list[i], 0);
return answer;
}
}
โ๏ธ Review
- ๊ฐ๋จํ ๊ตฌํ ๋ฌธ์ ์์ผ๋, ๋นํจ์จ์ ์ผ๋ก ๊ตฌํํ ๊ฒ ๊ฐ๋ค.